#include "stdafx.h"
#include "ListDetails.h"
#include "MenuBar.h"
#include "GraphicFunctions.h"

#ifdef _DEBUG
#include "crtdbg.h"
#endif

CListDetails::CListDetails(HWND hParent)
  : CList(hParent, Theme::iDetailsItemSize)
  , m_oId(0), m_eNoPic(siNone)
{
  m_szName[0] = 0;

  style.clrBackground1 = Theme::rgbDetailsBackground1;
  style.clrBackground2 = Theme::rgbDetailsBackground2;
  style.hbrBackground  = Theme::hbrDetailsBackground;
}

void CListDetails::Clear()
{
  m_oId = 0;
  m_eNoPic = siNone;
  m_szName[0] = 0;
  CList::Clear();
}

bool CListDetails::OnMouseDown(int x, int y)
{
  CList::OnMouseDown(x, y);

  if (!isMinimizing && x > rect.right - Theme::iListScrollbarWidth)
  {
    longTapTimer.Stop();
    SetSelection();
    SetHover();
    ScrollBar(y - rect.top);
  }

  return true;
}

bool CListDetails::OnMouseMove(int x, int y)
{
  if (isMinimizing && 
      x > rect.right - Theme::iListScrollbarWidth &&
      abs(y - rect.top - lastY) > Settings::scrollThresh)
  {
    longTapTimer.Stop();
    SetSelection();
    SetHover();
    isMinimizing = false;
    ScrollBar(y - rect.top);
    return true;
  }
  if (isScrolling)
  {
    ScrollBar(y - rect.top);
    return true;
  }

  return CList::OnMouseMove(x, y);
}

void CListDetails::Create(CEOID oId, ESpecialIcons eNoPic, const TCHAR* szName)
{
  Clear();

  m_oId = oId;
  m_eNoPic = eNoPic;
  ::StringCchCopy(m_szName, PRIMARY_TEXT_LENGTH, szName);
}

bool CListDetails::AddHeader(const TCHAR* name, const TCHAR* company, const TCHAR* department, const TCHAR* position, const TCHAR* birthday, const TCHAR* anniversary)
{
  CListItemDetailHeader* pItem = new CListItemDetailHeader(name, company, department, position, birthday, anniversary, m_oId, m_eNoPic);

  if (pItem)
  {
    Add(pItem);
    return true;
  }
  return false;
}

bool CListDetails::AddAction(ItemPropertyType type, const TCHAR* text, ListItemAction action, ListItemAction subAction, bool isDefault, CEPROPID propId)
{
  CListItemDetailAction* pItem = new CListItemDetailAction(type, m_szName,
    text, action, subAction, isDefault, m_oId, propId);
  if (pItem)
  {
    Add(pItem);
    return true;
  }
  return false;
}

bool CListDetails::AddString(ItemPropertyType type, const TCHAR* string)
{
  CListItemDetailString* pItem = new CListItemDetailString(type, string);
  if (pItem)
  {
    Add(pItem);
    return true;
  }
  return false;
}

bool CListDetails::AddText(ItemPropertyType type, const TCHAR* text)
{
  CListItemDetailText* pItem = new CListItemDetailText(type, text);
  if (pItem)
  {
    Add(pItem);
    return true;
  }
  return false;
}

bool CListDetails::AddRingtone(const TCHAR* text)
{
  CListItemDetailRingtone* pItem = new CListItemDetailRingtone(text);
  if (pItem)
  {
    Add(pItem);
    return true;
  }
  return false;
}

void CListDetails::Prepare(HDC hdc, const RECT& rc)
{
  SetRect(rc);

  int w = rect.right - rect.left;
  size_t n = items.size();
  for (size_t i = 0; i < n; i++)
  {
    ((CListItemDetail*)items[i])->Prepare(hdc, w, Theme::iDetailsItemSize);
  }

  CalculateHeights();
}

bool CListDetails::OnKeyDown(int vKey, bool bRepeating)
{
  if (CList::OnKeyDown(vKey, bRepeating))
    return true;

  switch(vKey)
  {
  case VK_LEFT:
    PostCommand(parentWnd, CMD_DETAILS_BACK);
    return true;
  }

  return false;
}

void CListDetails::ScrollBar(int y)
{
  if (height <= rect.bottom - rect.top)
    return;

  StopScrolling();

  isScrolling = true;
  abortKineticScroll = true;
  scrollSpeed = 25;

  double pct = (double)y / (double)(rect.bottom-rect.top);
  scrolled = (int)(scrollMax * pct);

  scrolled = min(scrolled, scrollMax);
  scrolled = max(scrolled, 0);

  Redraw();
}

void CListDetails::DrawOverlays(HDC hdc, HDC hdcTmp, const RECT& rClip)
{
  if (items.size() == 0)
    return;

  // Draw Scrollbar
  if (Theme::iListScrollbarIndicatorWidth > 0)
    DrawScrollBar(hdc, rClip);
}

void CListDetails::DrawScrollBar(HDC hdc, const RECT& rClip)
{
  RECT rBar = rect;
  rBar.left = rBar.right - Theme::iListScrollbarIndicatorWidth;
  FillRect(hdc, &rBar, Theme::hbrListBackground);

  if (height == 0)
    return;

  rBar.top = rect.top + scrolled * (rect.bottom-rect.top) / height;
  rBar.bottom = rBar.top + (rect.bottom-rect.top) * (rect.bottom-rect.top) / height;
  FillRect(hdc, &rBar, Theme::hbrListScrollbarIndicator);
}

